package com.fencing_squad.homo.slice;

import com.alibaba.fastjson.JSONObject;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.list.Comment;
import com.fencing_squad.homo.list.CommentProvider;
import com.fencing_squad.homo.list.PostUser;
import com.fencing_squad.homo.util.CustomComponent;
import com.fencing_squad.homo.util.Data;
import com.fencing_squad.homo.util.Network;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PostDetailSlice extends AbilitySlice {
    TextField comment;
    String post_message_id;
    CommentProvider provider;
    private CommonDialog tip = null;
    private Component send;
    private String commentText = "";
    private Component bigest;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_post_detail, null, false));

        post_message_id = intent.getStringParam("post_message_id");
        if (post_message_id == null) {
            terminate();
        }
        else {
            initConfig();
        }
    }

    private void like(Component component){
        JSONObject jsonObject = new JSONObject();
        System.out.println(post_message_id);
        jsonObject.put("post_message_id", post_message_id);

        Network.postJson("/post/like", jsonObject.toJSONString(), this::resolve, this::reject);
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    private void initConfig() {
        ListContainer container = (ListContainer) findComponentById(ResourceTable.Id_postCommentList);
        comment = (TextField) findComponentById(ResourceTable.Id_comment);
        send = findComponentById(ResourceTable.Id_send);
        Text barTitle = (Text) findComponentById(ResourceTable.Id_barTitle);
        Component barBack = findComponentById(ResourceTable.Id_barBack);
        barTitle.setText("帖子详情");
        barBack.setClickedListener(component -> {
            terminate();
        });

        Map<String, String> params = new ConcurrentHashMap<>();
        params.put("post_message_id", post_message_id);
        Network.get("/post/detail", params, this::resolve, this::reject);

        provider = new CommentProvider(this, null, this::likeResolve);
        container.setItemProvider(provider);

        comment.setFocusChangedListener(new Component.FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean b) {
                comment.setAdjustInputPanel(b);
            }
        });

        comment.addTextObserver((s, i, i1, i2) -> {
            this.commentText = s;
        });

        send.setClickedListener(component -> {
            if (!Data.Account.isLogin) {
                present(new GuideSlice(), new Intent());
                return;
            }
            if (commentText != null && !commentText.equals("")) {
                System.out.println(post_message_id);
                System.out.println(commentText);
                JSONObject object = new JSONObject();
                object.put("post_message_id", post_message_id);
                object.put("content", commentText);
                Network.postJson("/post/comment", object.toJSONString(), this::commentResolve, this::reject);
            }
        });
    }

    public void commentResolve(JSONObject object) {
        provider.clear();
        Map<String, String> params = new ConcurrentHashMap<>();
        params.put("post_message_id", post_message_id);
        Network.get("/post/detail", params, this::resolve, this::reject);
    }

    public void likeResolve() {
        commentResolve(null);
    }

    public void resolve(JSONObject object) {
        JSONObject data = object.getJSONObject("data");
        provider.setPostUser(new PostUser(data.getJSONObject("post_message")));
        provider.append(Comment.getFromJSONArray(data.getJSONArray("comment")));
    }

    public void reject(int code, String err) {
        if (code == 404) {
            tip = CustomComponent.getDialog(getContext(), err, () -> {
                if (tip != null) {
                    tip.hide();
                }
            }, () -> {
                if (tip != null) {
                    tip.hide();
                }
            });
            tip.show();
        }
        else {
            System.out.println("my err " + err);
        }
    }

    public static ArrayList<Comment> test() {
        ArrayList<Comment> list = new ArrayList<>();
        for(int i=0; i<10; i++) {
            Comment comment = new Comment();
            comment.setContent("contentttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" + String.valueOf(i));
            comment.setAvatar("");
            comment.setNickname("nick" + String.valueOf(i));
            list.add(comment);
        }
        return list;
    }
}
